//
//  GKZYUniversityCustomCircleView.m
//  GKZYAid
//
//  Created by Tony Zhang on 16/3/14.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYUniversityCustomCircleView.h"

@interface GKZYUniversityCustomCircleView (){
    
    CAShapeLayer *backGroundLayer; //背景图层
    CAShapeLayer *frontFillLayer;      //用来填充的图层
    UIBezierPath *backGroundBezierPath; //背景布赛尔曲线
    UIBezierPath *frontFillBezierPath;  //用来填充的布赛尔曲线
}

@end

@implementation GKZYUniversityCustomCircleView

@synthesize progressColor = _progressColor;
@synthesize progressTrackColor = _progressTrackColor;
@synthesize progressValue = _progressValue;
@synthesize progressStrokeWidth = _progressStrokeWidth;

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    
    }
    return self;
}

-(void)addViews{
    
    UILabel *markLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 40)];
    markLab.center = self.center;
    markLab.backgroundColor = [UIColor clearColor];
    markLab.textColor = [UIColor orangeColor];
    NSString *str = @"%";
    markLab.textAlignment = NSTextAlignmentCenter;
    markLab.font = [UIFont systemFontOfSize:25];
    NSString *string = [NSString stringWithFormat:@"%.0f",self.progressValue*100];
    markLab.text = [string stringByAppendingString:str];
    [self addSubview:markLab];
    
}


/**
 *  初始化创建图层
 */
- (void)setUp
{
    //创建背景图层
    backGroundLayer = [CAShapeLayer layer];
    backGroundLayer.fillColor = nil;
    backGroundLayer.frame = self.bounds;
    
    //创建填充图层
    frontFillLayer = [CAShapeLayer layer];
    frontFillLayer.fillColor = nil;
    frontFillLayer.frame = self.bounds;
    
    [self.layer addSublayer:backGroundLayer];
    [self.layer addSublayer:frontFillLayer];
}


- (void)setProgressColor:(UIColor *)progressColor
{
    _progressColor = progressColor;
    frontFillLayer.strokeColor = progressColor.CGColor;
}


- (UIColor *)progressColor
{
    return _progressColor;
}


- (void)setProgressTrackColor:(UIColor *)progressTrackColor
{
    _progressTrackColor = progressTrackColor;
    backGroundLayer.strokeColor = progressTrackColor.CGColor;
    backGroundBezierPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(CGRectGetWidth(self.bounds)-self.progressStrokeWidth)/2.f startAngle:0 endAngle:M_PI*2 clockwise:YES];
    backGroundLayer.path = backGroundBezierPath.CGPath;
}


- (UIColor *)progressTrackColor
{
    return _progressTrackColor;
}


- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    frontFillBezierPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(CGRectGetWidth(self.bounds)-self.progressStrokeWidth)/2.f startAngle:-M_PI_2 endAngle:(2*M_PI)*progressValue-M_PI_2 clockwise:YES];
    frontFillLayer.path = frontFillBezierPath.CGPath;
    [self addViews];
}


- (CGFloat)progressValue
{
    return _progressValue;
}


- (void)setProgressStrokeWidth:(CGFloat)progressStrokeWidth
{
    _progressStrokeWidth = progressStrokeWidth;
    frontFillLayer.lineWidth = progressStrokeWidth;
    backGroundLayer.lineWidth = progressStrokeWidth;
}


- (CGFloat)progressStrokeWidth
{
    return _progressStrokeWidth;
}


@end
